<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Manage extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
// Load model m_data lên
        $this->load->model('m_loaihoa');
        $this->load->model('m_mausac');
        $this->load->model('m_hoa');
    }

    function index() {
        $this->load->view('controlpanel');
        //echo $this->m_hoa->getLastProduct();
        //$this->load->view('controlpanel');
    }

    function image_upload() {

        $config['upload_path'] = './assets/imgs/Products/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '2000';
        $config['max_width'] = '1366';
        $config['max_height'] = '768';

        $this->load->library('upload', $config);

        if (!$this->upload->do_upload()) {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('products_manage', $error);
        } else {
            $data = array('upload_data' => $this->upload->data());

            $this->load->view('products_manage', $data);
        }
    }

    function load_add_page() {
        $data['category'] = $this->m_loaihoa->getAllCategory();
        $data['color'] = $this->m_mausac->getAllColor();
//        $data['mota'] = array(
//            'name' => 'MoTa',
//            'id' => 'MoTa',
//            'value' => '',
//            'rows' => '5',
//            'cols' => '10',
//            'style' => 'width:300px;height:50px;',
//        );
//        $data['nguyenlieu'] = array(
//            'name' => 'NguyenLieu',
//            'id' => 'MoTa',
//            'value' => '',
//            'rows' => '5',
//            'cols' => '10',
//            'style' => 'width:300px;height:50px;',
//        );

        $this->load->view('addproduct', $data);
    }

    function forward() {
        if ($this->input->post('edit') != '') {
            $data['MaHoa'] = $this->input->post('MaHoa');
            $data['category'] = $this->m_loaihoa->getAllCategory();
            $data['color'] = $this->m_mausac->getAllColor();
            $this->load->view('updateproduct', $data);
        } else if ($this->input->post('delete') != '') {
            if ($this->delete_data()) {
                echo ($this->input->post('MaHoa') . ' deleted');
            }
        }
    }

    function update_data() {
        $MaHoa = $this->input->post('MaHoa');
        $config['upload_path'] = './assets/imgs/Products/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '2000';
        $config['max_width'] = '1366';
        $config['max_height'] = '768';
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload()) {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('products_manage', $error);
        } else {
            $upload_data = $this->upload->data();
            $HinhAnh = 'imgs/Products/' . $upload_data['file_name'];
            $data = array(
                'TenHoa' => $this->input->post('TenHoa'),
                'MaLoaiHoa' => $this->m_loaihoa->getMaLoaiHoa($this->input->post('LoaiHoa')),
                'MaMauSac' => $this->m_mausac->getMaMauSac($this->input->post('MauSac')),
                'HinhAnh' => $HinhAnh,
                'Soluong' => $this->input->post('SoLuong'),
                'DonGia' => $this->input->post('DonGia')
            );
            $this->m_hoa->updateProduct($MaHoa, $data);
            $this->load->view('info');
        }
    }

    function insert_data() {
        $config['upload_path'] = './assets/imgs/Products/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '2000';
        $config['max_width'] = '1366';
        $config['max_height'] = '768';
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload()) {
            $error = array('error' => $this->upload->display_errors());
            //$data['msg'] = "Lỗi";                
            $this->load->view('info_error', $error);
        } else {
            $upload_data = $this->upload->data();
            $HinhAnh = 'imgs/Products/' . $upload_data['file_name'];
            $data = array(
                'MaHoa' => $this->m_hoa->getLastProduct() + 1,
                'TenHoa' => $this->input->post('TenHoa'),
                'MaLoaiHoa' => $this->m_loaihoa->getMaLoaiHoa($this->input->post('LoaiHoa')),
                'MaMauSac' => $this->m_mausac->getMaMauSac($this->input->post('MauSac')),
                'HinhAnh' => $HinhAnh,
                'Soluong' => $this->input->post('SoLuong'),
                'DonGia' => $this->input->post('DonGia'),
                'MoTa' => nl2br(trim($this->input->post('MoTa'))),
                'NguyenLieu' => nl2br(trim($this->input->post('NguyenLieu'))),
                'NgayNhap'=>date('Y-m-d')
            );
            if ($this->m_hoa->insertProduct($data)) {
                $data['msg'] = "Đã Thêm Hoa";
                $data['pg'] = "load_manage_page";
                $this->load->view('info_success', $data);
            } else {
                $data['msg'] = "Lỗi";
                $this->load->view('info_error', $data);
            }
        }
    }

    function delete_data() {
        $MaHoa = $this->input->post('MaHoa');
        $this->m_hoa->deleteProduct($MaHoa);
    }

    function insert_loaihoa() {
        $data = array(
            'MaLoaiHoa' => $this->m_loaihoa->getLastLoaihoa() + 1,
            'TenLoaiHoa' => $this->input->post('TenLoaiHoa'),
        );
        if ($this->m_loaihoa->insertLoaiHoa($data)) {
            $data['msg'] = "Đã Thêm Loại Hoa";
            $data['pg'] = "manage/load_add_page";
            $this->load->view('info_success', $data);
        } else {
            $data['msg'] = "Lỗi";
            $this->load->view('info_error', $data);
        }
    }

    function insert_mausac() {
        $data = array(
            'MaMauSac' => $this->m_mausac->getLastMauSac() + 1,
            'TenMauSac' => $this->input->post('MauSac'),
        );
        if ($this->m_mausac->insertMauSac($data)) {
            $data['msg'] = "Đã Thêm Màu Sắc";
            $data['pg'] = "manage/load_add_page";
            $this->load->view('info_success', $data);
        } else {
            $data['msg'] = "Lỗi";
            $this->load->view('info_error', $data);
        }
    }

    function order_detail() {
        $this->load->model('m_cthd');
        $this->load->model('m_hoadon');
        $MaHoaDon = $this->input->post('MaHoaDon');
        $detail = array();
        foreach ($this->m_cthd->getOrderDetail($MaHoaDon) as $item):
            $product = $this->m_hoa->getProduct($item['MaHoa']);
            array_push($detail, $product);
        endforeach;
        $data['items'] = $this->m_cthd->getOrderDetail($MaHoaDon);
        $data['detail'] = $detail;
        $order = $this->m_hoadon->getOrder($MaHoaDon);
        $data['order'] = $order;
        //echo $order->EmailNguoiDung;
        if ($this->session->userdata("MaQuyen") == 1) {
            $this->load->view('order_detail', $data);
        } else if ($this->session->userdata("MaQuyen") == 3) {
            $this->load->view('order_detail_user', $data);
        }
    }

    function checked_order() {
        $this->load->model('m_hoadon');
        $MaHoaDon = $this->input->post('id_order');
        if ($this->m_hoadon->updateStatusOrder($MaHoaDon)) {
            $data['msg'] = "Đã Nhận Hóa Đơn";
            $data['pg'] = "load_manage_orders";
            $this->load->view('info_success', $data);
        } else {
            $data['msg'] = "Có Lỗi Khi Nhận Hóa Đơn";
            $this->load->view('info_error', $data);
        }
    }

}
